/**
 * 创建人:  @author huangpeng    
 * 创建时间:  2023年01月07日 14:59
 * 项目名称:  lemon-blog-cloud
 * 文件名称:  BiUserController
 * 文件描述:  @Description: 普通用户controller
 * 公司名称:  柠檬
 *
 * All rights Reserved, Designed By 柠檬 
 * @Copyright: 2022-2023
 *
 */
package com.lemon.blog.web.controller;

import com.lemon.blog.web.vo.ResetPwdVO;
import com.lemon.common.core.web.controller.BaseController;
import com.lemon.common.core.web.domain.AjaxResult;
import com.lemon.common.core.web.page.TableDataInfo;
import com.lemon.common.log.annotation.Log;
import com.lemon.common.log.enums.BusinessType;
import com.lemon.blog.web.domain.BiUser;
import com.lemon.blog.web.service.BiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 包名称：com.lemon.blog.web.controller
 * 类名称：BiUserController
 * 类描述：普通用户controller
 * 创建人：@author huangpeng
 * 创建时间：2023年01月07日 14:59
 */
@RestController
@RequestMapping("/baseUser")
public class BiUserController extends BaseController {

    @Autowired
    private BiUserService biUserService;

    /**
     * 查询用户管理列表
     * @Title: list
     * @Description: 查询用户管理列表
     * @param biUser
     * @return: TableDataInfo
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 3:02
     */
    @GetMapping("/list")
    public TableDataInfo list(BiUser biUser)
    {
        startPage();
        List<BiUser> list = biUserService.selectBiUserList(biUser);
        return getDataTable(list);
    }

    /**
     * 根据id获取用户管理详细信息
     * @Title: getInfo
     * @Description: 根据id获取用户管理详细信息
     * @param id
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 3:01
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(biUserService.selectByPrimaryKey(id));
    }

    /**
     * 新增用户
     * @Title: add
     * @Description: 新增用户
     * @param biUser
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 3:01
     */
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BiUser biUser)
    {
        return toAjax(biUserService.insert(biUser));
    }

    /**
     * 修改用户
     * @Title: edit
     * @Description: 修改用户
     * @param biUser
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 3:01
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BiUser biUser)
    {
        return toAjax(biUserService.update(biUser));
    }

    /**
     * 根据用户id获取普通用户列表
     * @Title: queryUserListByKey
     * @Description: 根据用户id获取普通用户列表
     * @param userIds
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/1/8 16:28
     */
    @GetMapping(value = { "/queryBaseUserListByKey", "/queryBaseUserListByKey/{userIds}" })
    public AjaxResult queryBaseUserListByKey(@PathVariable(value = "userIds", required = false) Long[] userIds) {
        List<BiUser> userList = biUserService.queryBaseUserListByKey(userIds);
        return AjaxResult.success(userList);
    }

    /**
     * 重置密码
     * @Title: resetPwd
     * @Description: 重置密码
     * @param model
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 20:28
     */
    @Log(title = "前台用户管理重置密码", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody ResetPwdVO model)
    {
        return toAjax(biUserService.resetPwd(model));
    }

    /**
     * 前台新用户首次登录修改密码
     * @Title: newUserResetPassword
     * @Description: 前台新用户首次登录修改密码
     * @param model
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2025/4/10 15:00
     */
    @Log(title = "前台新用户首次登录修改密码", businessType = BusinessType.UPDATE)
    @PutMapping("/newUserResetPassword")
    public AjaxResult newUserResetPassword(@RequestBody ResetPwdVO model) {
        return toAjax(biUserService.newUserResetPassword(model));
    }

    /**
     * 获取用户扩展信息
     * @Title: getUserIllustrate
     * @Description: 获取用户扩展信息
     * @param userId 用户id
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/8/23 13:03
     */
    @GetMapping(value = { "/getUserIllustrate", "/getUserIllustrate/{userId}" })
    public AjaxResult getUserIllustrate(@PathVariable(value = "userId", required = false) Long userId) {
        Map<String, Object> userExtendMap = biUserService.getUserIllustrate(userId);
        return AjaxResult.success(userExtendMap);
    }


    /**
     * 前台查询用户列表,可以用于获取我的关注列表
     * @Title: queryFollowList
     * @Description: 前台查询用户列表,可以用于获取我的关注列表
     * @param biUser 查询参数
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/10/10 18:24
     */
    @GetMapping(value = "/queryFollowList")
    public TableDataInfo queryFollowList(BiUser biUser) {
        startPage();
        List<BiUser> followList = biUserService.queryFollowList(biUser);
        return getDataTable(followList);
    }

    /**
     * 判断用户编码是否存在
     * @Title: checkUserCode
     * @Description: 判断用户编码是否存在
     * @param userId 当前用户id
     * @param userCode 用户编码
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2025/4/11 10:09
     */
    @GetMapping("/checkUserCode")
    public AjaxResult checkUserCode(@RequestParam("userId") Long userId, @RequestParam("userCode") String userCode) {
        BiUser currUser = biUserService.selectByPrimaryKey(userId);
        if (currUser.getUserCode().equals(userCode)) {
            return AjaxResult.success(false);
        } else {
            BiUser biUser = biUserService.selectByUserCode(userCode);
            return AjaxResult.success(biUser != null);
        }
    }
}
